set more off

use dataset, clear

*** covariates ***

local controls dfederalaidpcp dstateaidpcp lpopulation pct_white pct_senior pct_renter lmedianincome 
local economy davgannualwage demployment dunemployment propvaluechange

*** make interactions ***

foreach x in overridep overridel `economy' dfederalaidpcp dstateaidpcp {
	su `x'
	gen demincumbentX`x' = demincumbent*`x'
	replace `x' = demincumbentX`x'
}
label variable overridep "Tax increase*Dem incumbent"
label variable davg "Average wages*Dem incumbent"
label variable propvalue "Property values*Dem incumbent"
label variable dunemployment "Unemployment*Dem incumbent"
label variable demployment "Employment*Dem incumbent"
label variable dstateaidpcp "State aid*Dem incumbent"
label variable dfederal "Federal grants*Dem incumbent"

*** do the regressions ***

keep if !mi(demvoteshare) & !mi(demincumbentXoverridep)
tabulate year, gen(year_)

foreach x in `controls' `economy' {
	su `x'
	replace `x' = (`x' - `r(min)') / (`r(max)' - `r(min)')
}

eststo clear

eststo: xi: reg demvoteshare lagdemvoteshare overridep year_* [aw=totalvotes], cl(town)

eststo: xi: reg demvoteshare lagdemvoteshare overridep davgann year_* [aw=totalvotes], cl(town)

eststo: xi: reg demvoteshare lagdemvoteshare overridep davgann demployment year_* [aw=totalvotes], cl(town)

eststo: xi: reg demvoteshare lagdemvoteshare overridep davgann demployment propvaluechange year_* if !mi(propvaluechange) [aw=totalvotes], cl(town)

eststo: xi: reg demvoteshare lagdemvoteshare overridep `economy' year_* [aw=totalvotes], cl(town)

eststo: xi: reg demvoteshare lagdemvoteshare overridep `economy' `controls' year_* [aw=totalvotes], cl(town) 

#delimit;

esttab
	using "table 2.tex"
	,
		replace
		cells(b(fmt(2) star) se(par))
		eqlabels(none)
		nomtitles
		collabels(none)
		order(overridepassed davgannualwage demployment propvaluechange dunemployment dfederalaidpcp dstateaidpcp)
		drop(*year*)
		gap
		indicate(
			"Lagged Democratic vote share = lagdemvoteshare"
			"Year fixed effects = year_1"			
		)
		label
		mlabels(none)
		nonotes
		booktabs
		width(\hsize)
		stats(
			N
			N_clust
			,
				label(
					"Observations"
					"Towns"
						
				)
				fmt(%20.0fc)
		)
		;
	
#delimit cr

esttab, keep(*overridep*) se compress
